<template>
  <div class="music-list">
    <div class="back" @click="goBack">
      <i class="icon-back"></i>
    </div>
    <h1 class="title">{{ title }}</h1>
    <div class="bg-image" :style="bgImageStyle" ref="bgImage">
      <div class="play-btn-wrapper" :style="playBtnStyle">
        <div @click="random" v-show="songs.length > 0" class="play-btn">
          <i class="icon-play"></i>
          <span class="text">随机播放全部</span>
        </div>
      </div>
      <!-- filter盒子相当于在背景图片盒子上盖了一层蒙版,用户模糊背景图片 -->
      <div class="filter" :style="filterStyle"></div>
    </div>
    <!-- :probe-type="3"实时获取滚动位置 -->
    <scroll
      :probe-type="3"
      class="list"
      :style="scrollStyle"
      v-loading="loading"
      v-no-result:[noResultText]="noResult"
      @scroll="onScroll"
    >
      <div class="song-list-wrapper">
        <song-list :rank="rank" :songs="songs" @select="selectItem"></song-list>
      </div>
    </scroll>
  </div>
</template>

<script>
// import scroll from '../base/scroll/scroll.vue';
import scroll from '../../components/wrap-scroll';
import songList from '../base/song-list/song-list';
import { mapActions, mapState } from 'vuex';

const RESERVED_HEIGHT = 40;

export default {
  components: {
    scroll,
    songList
  },
  computed: {
    ...mapState(['playList']),
    playBtnStyle() {
      // 随机播放按钮,在播放列表滑动到顶部时会出现在最顶上,因为使用了定位
      let display = '';
      // 当播放列表滚动到顶部之后(顶部title栏底部),隐藏随机播放按钮
      if (this.scrollY > this.maxTranslateY) {
        display = 'none';
      }
      return {
        display
      };
    },
    noResult() {
      // loding 结束且songs为空
      return !this.loading && !this.songs.length;
    },
    bgImageStyle() {
      const scrollY = this.scrollY;
      let zIndex = 0;
      let paddingTop = '70%';
      let height = 0; // 动态设置高度
      let translateZ = 0; // ios设备z-index失效解决方案

      // 当滚动距离大于最高可滚动的距离之后,增加index层级
      if (scrollY > this.maxTranslateY) {
        zIndex = 10;
        paddingTop = 0;
        height = `${RESERVED_HEIGHT}px`;
        translateZ = 1;
      }

      // 下拉歌曲列表图片放大
      let scale = 1; // 缩放
      // scrollY < 0表示正在向下拉屏幕
      if (scrollY < 0) {
        // 基础为1,需要在1的基础上加值
        scale = 1 + Math.abs(scrollY / this.imageHeight);
      }

      return {
        scale,
        transform: `scale(${scale}) translateZ(${translateZ}px)`,
        height,
        paddingTop,
        zIndex,
        backgroundImage: `url(${this.pic})`
      };
    },
    scrollStyle() {
      const bottom = this.playList.length ? '60px' : 0;
      return {
        top: `${this.imageHeight}px`,
        bottom
      };
    },
    filterStyle() {
      let blur = 0; // 模糊值
      const scrollY = this.scrollY;
      const imageHeight = this.imageHeight;
      // scrollY >= 0表示正在向上划
      if (scrollY >= 0) {
        // 模糊度需要有一个最大值
        // 最大值为滚动到距离顶部还有40像素的位置this.maxTranslateY / imageHeight
        // scrollY / imageHeight正常值
        blur =
          Math.min(this.maxTranslateY / imageHeight, scrollY / imageHeight) *
          20;
      }
      return {
        backdropFilter: `blur(${blur}px)`
      };
    }
  },
  props: {
    songs: {
      type: Array,
      default() {
        return [];
      }
    },
    loading: Boolean,
    title: String,
    pic: String,
    noResultText: {
      type: String,
      default: '抱歉,没有找到可播放的歌曲'
    },
    rank: Boolean
  },
  data() {
    return {
      imageHeight: 0, // 图片高度,初始为0
      scrollY: 0, // y轴滚动的距离
      maxTranslateY: 0 // 最大可以滚动的距离(纵坐标)
    };
  },
  mounted() {
    // 拿到图片高度
    this.imageHeight = this.$refs.bgImage.clientHeight;
    // 可以滚动到最大距离,图片高度减去顶部top栏高度
    this.maxTranslateY = this.imageHeight - RESERVED_HEIGHT;
  },
  methods: {
    random() {
      this.randomPlay(this.songs);
    },
    selectItem({ song, index }) {
      console.log('歌曲点击');
      this.selectplay({
        list: this.songs,
        index
      });
    },
    goBack() {
      this.$router.back();
    },
    onScroll(e) {
      // console.log(e);
      // 派发的值为一个负数,需要取反,获得正数
      this.scrollY = -e.y;
    },
    ...mapActions(['selectplay', 'randomPlay'])
  }
};
</script>

<style lang="scss" scoped>
.music-list {
  position: relative;
  height: 100%;
  .back {
    position: absolute;
    top: 0;
    left: 6px;
    z-index: 20;
    transform: translateZ(2px);
    .icon-back {
      display: block;
      padding: 10px;
      font-size: $font-size-large-x;
      color: $color-theme;
    }
  }
  .title {
    position: absolute;
    top: 0;
    left: 10%;
    width: 80%;
    z-index: 20;
    transform: translateZ(2px);
    @include no-wrap();
    text-align: center;
    line-height: 40px;
    font-size: $font-size-large;
    color: $color-text;
  }
  .bg-image {
    position: relative;
    width: 100%;
    transform-origin: top;
    background-size: cover;
    .play-btn-wrapper {
      position: absolute;
      bottom: 20px;
      z-index: 20;
      width: 100%;
      .play-btn {
        box-sizing: border-box;
        width: 135px;
        padding: 7px 0;
        margin: 0 auto;
        text-align: center;
        border: 1px solid $color-theme;
        border-radius: 100px;
        font-size: 0;
      }
      .icon-play {
        display: inline-block;
        vertical-align: middle;
        margin-right: 6px;
        font-size: $font-size-medium-x;
        color: $color-theme;
      }
      .text {
        display: inline-block;
        vertical-align: middle;
        font-size: $font-size-small;
        color: $color-theme;
      }
    }
    .filter {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: rgba(7, 17, 27, 0.4);
    }
  }
  .list {
    position: absolute;
    bottom: 0;
    width: 100%;
    z-index: 0;
    .song-list-wrapper {
      padding: 20px 30px;
      background: $color-background;
    }
  }
}
</style>
